import BaseCURDV2Api from '@/framework/infra/base/BaseCURDV2Api'
import VoteBadCommentStatusEnum from '@/feature/cmt/infra/constants/VoteBadCommentStatusEnum'
import ApiAdaptor from '@/framework/infra/base/ApiAdaptor'
import store from '@/store'

class VoteBadCommentApi extends BaseCURDV2Api {
  constructor() {
    super('cmt/vote-bad-comment')
  }

  follow(data) {
    return this.post('follow', data)
  }

  submit(id) {
    return this.post('submit', { id })
  }

  dataCount() {
    return this.get('data-count').then(data => {
      if (!data) {
        return {}
      }
      return this.parseDataCount(data)
    })
  }

  parseDataCount(data) {
    const map = data.reduce((r, a) => {
      let status = a.status
      if (status === VoteBadCommentStatusEnum.ING.id) {
        status = VoteBadCommentStatusEnum.WAIT_FOLLOW.id
      }
      r[status] = r[status] || 0
      r[status] += a.count
      return r
    }, {})
    return VoteBadCommentStatusEnum.getKeys().reduce((r, a) => {
      r[a] = r[a] || 0
      r[a] += map[VoteBadCommentStatusEnum[a].id] || 0
      return r
    }, {})
  }

  followReject(data) {
    return this.post('follow-reject', data)
  }

  listCanFollow(data) {
    return this.get('list-can-follow', data)
  }

  listMyFollow(data) {
    return this.get('list-my-follow', data)
  }

  listCanCheck(data) {
    return this.get('list-can-check', data)
  }

  queryTotalRevocationRate(data = {}) {
    return this.get('total-revocation-rate', data)
  }

  queryRevocationRateByStore({ dateRange: data, ...param }) {
    if (data && data.length) {
      param.startDate = data[0]
      param.endDate = data[1]
    }
    return this.get('revocation-rate-by-store', param)
  }

  _arrSum(arr, startIdx, endIdx) {
    return arr.slice(startIdx, endIdx).reduce((r, a) => {
      return r + a
    }, 0)
  }

  queryRevocationRateByStoreAndFollowerId({ dateRange: data, ...param }) {
    if (data && data.length) {
      param.startDate = data[0]
      param.endDate = data[1]
    }
    return this.get('revocation-rate-by-store-follower', param).then(data => {
      let storeId = ''
      let firstIdx = 0
      const results = []
      for (let idx = 0; idx < data.length; idx++) {
        const item = data[idx]
        if (storeId === '') {
          storeId = item.storeId
        } else if (storeId !== item.storeId) {
          data[firstIdx].rowSpan = idx - firstIdx
          storeId = item.storeId
          results.push({
            storeTitle: '汇总',
            storeTitleColSpan: 2,
            rowSpan: 1,
            storeTotalNum: data[firstIdx].storeTotalNum,
            storeBackOutNum: data[firstIdx].storeBackOutNum,
            storeBadNum: data[firstIdx].storeBadNum,
            storePayOutAmount: data[firstIdx].storePayOutAmount,
            backOutNum: data[firstIdx].storeBackOutNum, // this._arrSum(data.map(el => el.backOutNum), firstIdx, idx - 1),
            payOutAmount: data[firstIdx].storePayOutAmount,
            payOutAmountPercent: '100%',
            backOutNumPercent: '100%'
          })
          firstIdx = idx
        }
        item.storeTitleColSpan = 1
        item.payOutAmountPercent += '%'
        item.backOutNumPercent += '%'
        results.push(item)
      }
      if (results && results.length) {
        data[firstIdx].rowSpan = data.length - firstIdx
        const item = data[firstIdx]
        results.push({
          storeTitle: '汇总',
          storeTitleColSpan: 2,
          rowSpan: 1,
          storeTotalNum: item.storeTotalNum,
          storeBackOutNum: item.storeBackOutNum,
          storeBadNum: item.storeBadNum,
          storePayOutAmount: item.storePayOutAmount,
          backOutNum: item.storeBackOutNum, // this._arrSum(data.map(el => el.backOutNum), firstIdx, idx - 1),
          payOutAmount: item.storePayOutAmount,
          payOutAmountPercent: '100%',
          backOutNumPercent: '100%'
        })
      }
      return results
    })
  }

  queryRevocationRateByFollowerId({ dateRange: data, ...param }) {
    if (data && data.length) {
      param.startDate = data[0]
      param.endDate = data[1]
    }
    return this.get('revocation-rate-by-follower', param)
  }

  list(params) {
    const getParams = { ...params }
    Object.keys(getParams).forEach(key => {
      if (Array.isArray(getParams[key])) {
        getParams[key] = getParams[key].join(',')
      }
    })
    return this.post('list-page', getParams)
  }
}

export const voteBadCommentApi = new VoteBadCommentApi()
